package worklog;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class DaysCalculator {
	
	/**
	 * Retrieves an array of dates for "from - to" range
	 */
	public static String[] getDays(String fromDateStr, String toDateStr){
		ArrayList<String> days = new ArrayList<String>();
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date fromDate = null;
		Date toDate = null;
		
		try { fromDate = (Date)formatter.parse(fromDateStr); } catch (ParseException e){}
		try { toDate = (Date)formatter.parse(toDateStr); } catch (ParseException e){}
		
		if (fromDate == null || toDate == null) return days.toArray(new String[0]);
	
		Calendar startDate = Calendar.getInstance(); startDate.setTime(fromDate);
		Calendar endDate = Calendar.getInstance(); endDate.setTime(toDate);
	
		if (startDate.compareTo(endDate) == 1){					// endDate is before startDate - reverse them
			Calendar tmp = startDate;
			startDate = endDate;
			endDate = tmp;
		}    	
		
		int daysBetw = daysBetween(startDate, endDate);
		
		for (int i = 0; i < daysBetw; i++){
			days.add(formatter.format(startDate.getTime())); 
			startDate.add(Calendar.DAY_OF_YEAR, 1);
		}
		
		return days.toArray(new String[days.size()]);
	}
	
	
	/**
	 * Calculate how many days is between 2 dates
	 */
	public static int daysBetween(Calendar startDate, Calendar endDate) {
		if (startDate.compareTo(endDate) == 0) return 1;		// dates are equal
		int daysBetween = 0;
		if (startDate.compareTo(endDate) == -1){				// endDate is after startDate
			Calendar date = (Calendar) startDate.clone();
			while (date.before(endDate)) {
				date.add(Calendar.DAY_OF_MONTH, 1);
				daysBetween++;
			}
		}
		if (startDate.compareTo(endDate) == 1){					// startDate is after endDate
			Calendar date = (Calendar) endDate.clone();
			while (date.before(startDate)) {
				date.add(Calendar.DAY_OF_MONTH, 1);
				daysBetween++;
			}
		}
		return daysBetween+1;
	}
	
	
	/**
	 * Retrieves an array of week-day dates for a dateStr
	 * @param dateStr		string date (YYYY-MM-DD)
	 * @param showWeekends	if true - will return 7 days week instead of 5
	 */
	public static String[] getWeekDates(String dateStr, Boolean showWeekends){
		int dayNo = 5;
		if (showWeekends == true) dayNo = 7;
		String[] weekDays = new String[dayNo];
		
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
    	try { date = (Date)formatter.parse(dateStr); } catch (ParseException e){}
    	if (date == null) return weekDays;
    	
    	// get Monday from date
    	Calendar cal = Calendar.getInstance();
    	cal.setTime(date);
    	int weekday = cal.get(Calendar.DAY_OF_WEEK);
    	if (weekday == Calendar.SUNDAY) cal.add(Calendar.DAY_OF_YEAR, -6);
    	else if (weekday != Calendar.MONDAY) cal.add(Calendar.DAY_OF_YEAR, 2 - weekday);
    	
    	for (int i = 0; i < dayNo; i++){
    		weekDays[i] = formatter.format(cal.getTime()); 
    		cal.add(Calendar.DAY_OF_YEAR, 1);
    	}
    	
    	return weekDays;
	}

	
	/**
	 * Get string date x days ago
	 */
	public static String getDateDaysAgo(int days){
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		if (days > 0) cal.add(Calendar.DAY_OF_YEAR, -days);
		return formatter.format(cal.getTime());
	}	
	
	
	
	/**
	 * Convert String date to unix timestamp
	 */
	public static long strToTime(String dateStr){
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
    	try { date = (Date)formatter.parse(dateStr); } catch (ParseException e){}
    	if (date == null) return 0;
    	
    	Calendar cal = Calendar.getInstance();
    	cal.setTime(date);
    	
    	return cal.getTimeInMillis();
	}
	
}